
package chess;

import game.Game;

import java.util.ArrayList;


/**
 * Board:
 * 	This class is used to represent a chess board. It consists of a grid of tiles.
 * It is responsible for knowing which tiles are on the grid.
 * <pre>
 * Max Joyce
 * -Constructed Class(Jan 21,2010)
 * Andrew Jeffery
 * -Documented Class(01/22/10)
 * -Added Game Parameter(01/22/10)
 * </pre>
 */


public class Board {

	
	private ArrayList<ArrayList<Tile>> tiles;
	private Game game;
	
	public static final int BOARD_DIMENSION=8;

	/**
	 * <pre>
	 * Changed Jan 24,2010 By Caleb
	 * Error when initialising tiles. Changed get(j) to get(i)
	 * </pre>
	 */
	public Board(Game newGame) {
		tiles = new ArrayList<ArrayList<Tile>>();
		this.game=newGame;
		for (int i=0;i<Board.BOARD_DIMENSION;i++){
			tiles.add(new ArrayList<Tile>());
		}
		for (int i=0;i<Board.BOARD_DIMENSION;i++){
			for (int j=0;j<Board.BOARD_DIMENSION;j++){
				tiles.get(i).add(new Tile(this,i,j)); // 
			}
		}
	}
	
	/**
	 * Getter to retrieve the tile object at a certain location on the board.
	 * <pre>
	 * Max Joyce
	 * -Constructed Method(01/21/10)
	 * Andrew Jeffery
	 * -Documented Method(01/22/10)
	 * </pre>
	 * @param point object, the tile location to be retrieved
	 * @return either returns the Tile object of interest or Null if tile does not exist
	 */
	public Tile getTile(int x, int y){
		return tiles.get(x).get(y);
	}	
	
	/**
	 * Outputs a text based representation of the board
	 * <pre>
	 * Max Joyce
	 * -Constructed Method(01/21/10)
	 * </pre>
	 */
	public void drawBoard(){
		game.getView().displayBoard();
	}
	/**
	 * Returns the all the tiles in the board in a grid format
	 * @return Returns all the tiles in the board
	 * <pre>
	 * Caleb Chan
	 * - Modified/Commented February 16, 2010
	 * </pre>
	 */
	public ArrayList<ArrayList<Tile>> getTiles() {
		// TODO Auto-generated method stub
		return tiles;
	}

	public Game getGame() {
		return game;
	}
	public String toString(){
		String s = "  --------------------------\n";
		for (int i=0;i<Board.BOARD_DIMENSION;i++){
			s += i+" |";
			for (int j=0;j<Board.BOARD_DIMENSION;j++){
				if (this.getTile(j,i).isPieceHere()){
					s += "["+this.getTile(j,i).getPieceHere().toString() + "]";
				}
				else{
					s +="[ ]";
				}
			}
			s +="\n";
		}
		s +="  --------------------------\n";
		s +="    0  1  2  3  4  5  6  7\n";
		return s;
	}
}
